﻿
$(document).ready(function () {

    // initialize()
    var geocoder = new google.maps.Geocoder();

    function codeLatLng(lat, lng) {
        var latlng = new google.maps.LatLng(lat, lng);
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    zipcode = (results[1]['address_components'][0]['long_name']);
                    $("#zipcode_textbox").val(zipcode);
                } else {
                    alternativeGeolocation();
                }
            } else {
                alternativeGeolocation();
            }
        });
    }

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            codeLatLng(position.coords.latitude, position.coords.longitude);
        }, function () {
            alternativeGeolocation();
        });
    } else {
        alternativeGeolocation();
    }

    var alternativeGeolocation = function () {
        //alert("No location support");
    }

});